package com.itheima.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import javax.sql.DataSource;


@Configuration
@PropertySource(value = "classpath:jdbc.properties")
public class JDBCConfig {
/*
<bean id="DruidDataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${jdbc.driver}"></property>
        <property name="url" value="${jdbc.url}"></property>
        <property name="username" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.pwd}"></property>
    </bean>
*/
    @Value("${jdbc.driver}")
    private String driverClassName;

    @Value("${jdbc.url}")
    private String url;

    @Value("${jdbc.username}")
    private String username;

    @Value("'${jdbc.pwd}'")
    private String password;

    @Bean("dataSource")
    public DataSource getDataSource(){
        DataSource dataSource=new DruidDataSource();
        ((DruidDataSource) dataSource).setDriverClassName(driverClassName);
        ((DruidDataSource) dataSource).setPassword(password);
        ((DruidDataSource) dataSource).setUrl(url);
        ((DruidDataSource) dataSource).setName(username);
        return dataSource;
    }
}
